import React, { Component, Fragment } from "react";
// antd
import { Table, Button, Col, Pagination, Form, Input, Tag } from "antd";
// http
import http from '../../utils/request';


class RdsAccountManage extends Component {
    constructor(props){
        super(props);
        this.state = {
            // 表格分页
            total: '',
            page: 1,
            page_number: 10,
            dataSource: [],
            columns: [
                {title: "云平台", dataIndex: "cloud-platform", key: "cloud-platform",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.platform}-{rowData.cloud_account_name}</Tag>                                
                            </div>
                        )
                    }
                },
                {title: "实例ID", dataIndex: "instance_id", key: "instance_id"},
                {title: "实例名称", dataIndex: "instance_name", key: "instance_name"},
                {title: "数据库", dataIndex: "dbname", key: "dbname"},
                {title: "账户名称", dataIndex: "account_name", key: "account_name"},
                {title: "账户属性", dataIndex: "account_privilege", key: "account_privilege"},
            ],
            loading: false,
            search_dbinstance_id: null,
            search_account_name: null,
        }
    }

    componentDidMount() {
        this.loadData()
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'dbinstance_id__contains': this.state.search_dbinstance_id,
            'account_name__contains': this.state.search_account_name,
        }

        // 获取分组列表
        http.get('/cloud/database/account', {params: requestData}).then(response=>{
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    /** 切换每页数量 */
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }

    /** 业务线搜索 */
    onFinishSearch = (data) => {
        this.setState({
            search_dbinstance_id: data.dbinstance_id ? data.dbinstance_id : null,
            search_account_name: data.account_name ? data.account_name : null,
        })
        this.loadData()
    }

    render(){
        const { loading, dataSource, columns, page, total } = this.state
        const rowSelection = {
            onChange: this.onCheckbox
        }
        var myStyle = {margin: '20px'}
        var myStyle1 = {margin: '10px', float: 'right'}
        return (
            <Fragment>
                <Form layout="inline" onFinish={this.onFinishSearch}>
                    <Form.Item label="实例ID" name="dbinstance_id">
                        <Input placeholder="请输入实例ID"  />
                    </Form.Item>
                    <Form.Item label="账户名称" name="account_name">
                        <Input placeholder="请输入账户名称"  />
                    </Form.Item>
                    <Form.Item shouldUpdate={true}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>
                
                <div style={myStyle1}><Button type="primary" onClick={this.onAddBusinessBtn}>添加业务线</Button></div>
                <Table
                    pagination={false} 
                    rowSelection={rowSelection} 
                    rowKey="id" 
                    columns={columns} 
                    dataSource={dataSource}
                    loading={loading}
                    scroll={{ y: 500, x: '70vw' }}
                >
                </Table>

                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>

            </Fragment>
        )
    }
}
export default RdsAccountManage;